package cn.xz.zysjz.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Objects;
import java.util.Random;

import cn.xz.zysjz.R;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.GetBean;
import cn.xz.zysjz.bean.UserBean;
import cn.xz.zysjz.bean.YyBean;
import cn.xz.zysjz.databinding.ActivityGetBinding;
import cn.xz.zysjz.databinding.ActivityIndexBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 预约界面*/
public class GetActivity extends AppCompatActivity {

    ActivityGetBinding mBinding;

    //来源数据
    GetBean mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_get);
        mData = (GetBean) getIntent().getSerializableExtra(CommonLib.JUMP_TYPE);
        initEvent();
        initData();
    }

    private void initData(){
        //选择的疫苗
        mBinding.tv1.setText("疫苗种类: " + mData.getActivityListBean().getName());
        //作用
        mBinding.tv2.setText("疫苗作用: " + mData.getActivityListBean().getMsg());
        //接种点
        mBinding.tv3.setText("接种点地址: " + mData.getIndexBean().getAddress());
        //联系方式
        mBinding.tv4.setText("接种点联系方式: " + mData.getIndexBean().getPhone());
        //负责医生
        mBinding.tv5.setText("负责医生: " + mData.getIndexBean().getName());
        //剩余
        mBinding.tv6.setText("当前疫苗剩余: " + mData.getIndexBean().getNumber() + "支/人");
        //是否免费
        String m = "自行前往接种点进行付费";
        if (mData.getActivityListBean().getFree()){
            m = "免费接种";
        }
        mBinding.tv7.setText("是否免费: " + m);
        //用户名
        if (CommonLib.nowUserBean != null){
            mBinding.tv8.setText("预约用户: " + CommonLib.nowUserBean.getName());
        }else {
            mBinding.tv8.setText("预约用户: 游客");
        }
        //手机号
        String mP = "用户手机号: 未绑定";
        if (!Objects.equals(SessionUtils.getString(CommonLib.USER_PHONE, ""), "")){
            mP = "用户手机号: " + SessionUtils.getString(CommonLib.USER_PHONE, "");
        }
        mBinding.tv9.setText(mP);
    }

    private void initEvent(){
        //返回
        mBinding.viewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //预约
        mBinding.tvYy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //是否绑定手机号
                if (Objects.equals(SessionUtils.getString(CommonLib.USER_PHONE, ""), "")){
                    ToastUtils.show("当前未绑定手机号，请先去绑定手机号");
                    return;
                }
                //预约成功
                int min = 1;
                int max = mData.getIndexBean().getNumber();
                Random random = new Random();
                int num = random.nextInt(max)%(max-min+1) + min;
                ArrayList<YyBean> list = SessionUtils.getList(CommonLib.USER_JL, YyBean.class);
                YyBean yyBean = new YyBean();
                yyBean.setNumber(num + "");
                Calendar instance = Calendar.getInstance();
                String toDay = instance.get(Calendar.YEAR) + "-" +(instance.get(Calendar.MONTH) + 1) + "-" + instance.get(Calendar.DAY_OF_MONTH) + "";
                yyBean.setDay(toDay);
                yyBean.setActivityListBean(mData.getActivityListBean());
                yyBean.setIndexBean(mData.getIndexBean());
                list.add(yyBean);
                //存储
                SessionUtils.commitList(CommonLib.USER_JL,list);

                ShowDialog.show("您已成功预约: " + mData.getActivityListBean().getName()
                        + ",排号为: " + num + ",请您于" + toDay
                        + "前往接种,详情信息请前往”我的预约查看“。", GetActivity.this, new ShowDialog.OnShowClick() {
                    @Override
                    public void click() {
                        finish();
                    }
                });
            }
        });
    }
}